package com.myteam.mystory.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.myteam.mystory.R;
import com.myteam.mystory.presention.ChapterActivity;

public class AllStoryAdapter extends BaseAdapter implements OnClickListener {
	private LayoutInflater mInflater;
	private Context context;
	private String[] data;
	
	TextView nameStory,nameAuthor,viewCount;
	ImageView img_avata,bookmark;

	public AllStoryAdapter(Context context) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	public void setData(String[] data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.length;
	}

	@Override
	public Object getItem(int item) {
		return data[item];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.allstory_adapter, null);
			holder = new ViewHolder();
			holder.mTitleText = (TextView) convertView.findViewById(R.id.allstory_title);
			holder.img_bookmark = (ImageView) convertView.findViewById(R.id.allstory_bookmark);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.mTitleText.setText(data[position]);
		nameStory = (TextView) convertView.findViewById(R.id.allstory_title);
		nameAuthor = (TextView) convertView.findViewById(R.id.allstory_author);
		img_avata = (ImageView) convertView.findViewById(R.id.allstory_img);
		bookmark = (ImageView) convertView.findViewById(R.id.allstory_bookmark);
		nameAuthor.setOnClickListener(this);
		nameStory.setOnClickListener(this);
		img_avata.setOnClickListener(this);
		bookmark.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(context);
				dialog.setMessage("Bạn có muốn lưu lại truyện này không?");
				dialog.setCancelable(false);
				dialog.setPositiveButton("Hủy", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
				dialog.setNegativeButton("Đồng ý", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						holder.img_bookmark.setImageResource(R.drawable.bookmark_active);
						//set bookmark
					}
				});
				dialog.create();
				dialog.show();
			}
		});
		
		return convertView;
	}

	static class ViewHolder {
		TextView mTitleText;
		ImageView img_bookmark;
	}
	
	
	private void clickView(){
		Intent intent = new Intent(context, ChapterActivity.class);
		context.startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.allstory_img:
			clickView();
			break;
		case R.id.allstory_title:
			clickView();
			break;
		case R.id.allstory_author:
			clickView();
			break;
		case R.id.allstory_bookmark:
			bookmark.setBackgroundResource(R.drawable.bookmark_active);
			Toast.makeText(context, "click bookmark", Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}
	}
}